from enum import Enum
from typing import Dict

from utils.CustomException import SqlTypeNotExist


class SqlTypesEnum(Enum):
    date_time = ["datetime", "timestamp"]
    date = ["date"]
    decimal = ["decimal", "double", "float"]
    string = ["char", "varchar", "text", "mediumtext", "longtext"]
    integer = ["int", "tinyint", "smallint"]
    long = ["bigint"]
    enum = ["enum"]


"""
根据sql类型枚举生成的sql到java类型的映射字典
"""
SQL_TO_JAVA_TYPE_MAP: Dict[str, str] = {}
for i in SqlTypesEnum:
    for j in i.value:
        SQL_TO_JAVA_TYPE_MAP[j] = {
            SqlTypesEnum.date_time: "Date",
            SqlTypesEnum.date: "Date",
            SqlTypesEnum.decimal: "BigDecimal",
            SqlTypesEnum.string: "String",
            SqlTypesEnum.integer: "Integer",
            SqlTypesEnum.long: "Long",
            SqlTypesEnum.enum: "Enum"
        }[i]


def sql_type_convert_to_java_type(sql_type: str) -> str:
    """
    sql类型转换为java类型
    :param sql_type: sql类型
    :return: java类型
    """
    if sql_type not in SQL_TO_JAVA_TYPE_MAP.keys():
        raise SqlTypeNotExist(f"sql类型'{sql_type}'不存在")
    else:
        return SQL_TO_JAVA_TYPE_MAP[sql_type]


if __name__ == "__main__":
    print(sql_type_convert_to_java_type("datetime"))
